﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;
using WebApplication1.domain;

namespace WebApplication1.sysAdministrator
{
    public partial class userManagement : System.Web.UI.Page
    {

        List<User> users = new List<User>();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.getList();
            }
            Panel1.Visible = false;
        }
        private void bind(DataTable Adt) {
            int row = Adt.Rows.Count;
            for (int r = 0; r < row; r++)
            {
                User user = new User();
                Role role = new Role();
                user.Id = (int)Adt.Rows[r][0];
                user.Usernmae = Adt.Rows[r][1].ToString();
                user.Password = Adt.Rows[r][2].ToString();
                user.RoleId = (int)Adt.Rows[r][3];
                role.Name = Adt.Rows[r][4].ToString();
                user.Role = role;
                users.Add(user);
            }
            GridView1.DataSource = users;
            GridView1.DataBind();
        
        }
        private void getList()
        {
            String sql = "select u.id id,u.username username,u.password password,role.id r_id,role.name role_name from user u left join role on u.role_id = role.id";
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            bind(Adt);
        }

        

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // DataRow row = MySqlHelper.GetDataRow(sql);
            int index = e.RowIndex;
            GridViewRow row = GridView1.Rows[index];
            string id = ((Label)row.FindControl("id")).Text;
            String sql = "delete from user where id = " + id;
            int result = MySqlHelper.delete(sql);
            if (result == 1)
            {
                Response.Write("<script>alert('删除成功！')</script>");
                getList();
            }
            else
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }
        }
        //让当前行处于修改状态
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            getList();
        }
        //取消编辑
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            getList();
        }
        //更新至数据库
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            GridViewRow row = GridView1.Rows[index];
            string id = ((Label)row.FindControl("id")).Text;
            string username = ((TextBox)row.FindControl("eusername")).Text;
            string password = ((TextBox)row.FindControl("epassoword")).Text;
            string rolename = ((DropDownList)row.FindControl("drpRole")).SelectedValue;
            String sql = "select * from role where name = '" + rolename+"'";
            DataTable data = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            int roleId = (int)data.Rows[0][0];
            String sql2 = "update user set username = '" + username + "',password = '" + password +
                  "' ,role_id = '" + roleId + "' where id = " + id;
            MySqlHelper.GetDataTable(sql2);
            Response.Write("<script>alert('更新成功');</script>");
            GridView1.EditIndex = -1;
            getList();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //Response.Write("当前行的下标：" + e.Row.RowIndex+"<br/>");
            //Response.Write("当前行的类型：" + e.Row.RowType.ToString() + "<br/>");
            //Response.Write("当前行的状态：" + e.Row.RowState.ToString() + "<br/>");
            string role = "";//保存当前正在绑定的学员的性别
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox username = e.Row.FindControl("eusername") as TextBox;
               
                if (username != null)
                {
                    String sql = "select u.id id,u.username username,u.password password,role.id r_id,role.name role_name from user u left join role on u.role_id = role.id where username = " + username.Text;
                    DataTable data = MySqlHelper.GetDataTable(sql);
                    //2.获取第一行数据
                    role = data.Rows[0][4].ToString();
                }
            }
            //普通项处于编辑状态,交替项的编辑状态
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))
            {
                DropDownList drpRole = e.Row.FindControl("drpRole") as DropDownList;
                if (drpRole != null)
                    drpRole.SelectedValue = role;//设置性别下拉列表的选中状态是当前学员的性别
            }
        }

        protected void add_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }

        protected void okAdd_Click(object sender, EventArgs e)
        {

            string username = addUsername.Text;
            string password = addPassword.Text;
            string rolename = addRole.SelectedValue;
            String sql = "select * from role where name = '" + rolename + "'";
            DataTable data = MySqlHelper.GetDataTable(sql);
            int roleId = (int)data.Rows[0][0];
            String sql2 = "insert into user (username,password,role_id) values ('" + username + "','" + password + "','" + roleId+"')";
            MySqlHelper.GetDataTable(sql2);
            Response.Write("<script>alert('添加成功');</script>");
            getList();
            Panel1.Visible = false;
        }

        protected void escAdd_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }

        protected void select_Click(object sender, EventArgs e)
        {
            string username = selectUsername.Text;
            String sql = "select u.id id,u.username username,u.password password,role.id r_id,role.name role_name from user u left join role on u.role_id = role.id where username = '"+username+"'";
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            bind(Adt);
        }

        protected void selectAll_Click(object sender, EventArgs e)
        {
            getList();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.getList();
        }
    }
}